package com.regvision.contacts.ui;

import java.util.List;

import com.google.gson.Gson;
import com.regvision.contacts.R;
import com.regvision.contacts.dialog.BaseDialog.OnCancelClickListeren;
import com.regvision.contacts.dialog.BaseDialog.OnConfirmClickListeren;
import com.regvision.contacts.dialog.ContactDialog;
import com.regvision.contacts.domain.ContactDataModel;
import com.regvision.contacts.main.ContactsApplication;
import com.regvision.contacts.mode.Observable;
import com.regvision.contacts.mode.Observer;
import com.regvision.contacts.utils.ActivityUtil;
import com.regvision.contacts.utils.ContactsUtils;
import com.regvision.contacts.utils.LogUtil;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

public class BaseActivity extends ActionBarActivity implements Observer {

	protected static String TAG = "BaseActivity";
	
    protected ActionBar actionBar;
    
	private ContactsApplication mContactsApplication;
	
	private String sharedContactInfo = null;
	private ContactDataModel sharedContactDataModel = null;
	private static final int HANDLE_HISTORY_CHANGED_EVENT = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		initActionBar();
		initData();
		ActivityUtil.add(this);
	}

	private void initActionBar() {

		actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayShowHomeEnabled(true);
		actionBar.setHomeButtonEnabled(true);
	}
	
	private void initData() {
		mContactsApplication = (ContactsApplication)getApplication();
	}

	protected void openActivity(Class<?> clazz) {
		try {
			Intent intent = new Intent(this, clazz);
			startActivity(intent);
		} catch (Exception e) {
			LogUtil.e(TAG, getString(R.string.error_open_activity) + clazz.getName());
			e.printStackTrace();
		}
	}

	protected Context getContext() {
		return this;
	}
	
	/* (non-Javadoc)
	 * @see android.support.v4.app.FragmentActivity#onPause()
	 */
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		mContactsApplication.deleteObserver(this);
		super.onPause();
	}

	/* (non-Javadoc)
	 * @see android.support.v4.app.FragmentActivity#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		mContactsApplication.addObserver(this);
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		ActivityUtil.remove(this);
		mContactsApplication.deleteObserver(this);
	}
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
	
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		Log.i(TAG, "update(" + data + ")");
	    String qualifier = (String)data;
	    if (qualifier.equals(mContactsApplication.HISTORY_CHANGED_EVENT)) {
            Message message = mHandler.obtainMessage(HANDLE_HISTORY_CHANGED_EVENT);
            mHandler.sendMessage(message);
        }
	}
	
	private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case HANDLE_HISTORY_CHANGED_EVENT:
                {
                    Log.i(TAG, "mHandler.handleMessage(): HANDLE_HISTORY_CHANGED_EVENT");
                    updateHistory();
                    break;
                }
            }
        }
	};
	
	private void updateHistory() {
		Log.i(TAG, "updateHistory()");
		List<String> messages = mContactsApplication.getHistory();
		if (messages.size() > 0) {
			sharedContactInfo = new String(messages.get(0));
//			Toast.makeText(this, sharedContactInfo, Toast.LENGTH_LONG).show();
			sharedContactDataModel = new Gson().fromJson(sharedContactInfo, ContactDataModel.class);
			showContactDialog(sharedContactDataModel);
			mContactsApplication.clearHistory();
		}
	}
	
	private void showContactDialog(final ContactDataModel model) {

		ContactDialog dialog = new ContactDialog(getContext());
		dialog.setTitle(model.name);
		dialog.setContactDataModel(model);
		dialog.setConfirmText("保存");
		dialog.setOnConfirmClickListeren(new OnConfirmClickListeren() {
			
			@Override
			public void onConfirmClick() {
				// TODO Auto-generated method stub
				ContactsUtils.saveContactInfo(getContext(), model);
				updateList();
			}
		});
		dialog.setOnCancelClickListeren(new OnCancelClickListeren() {
			
			@Override
			public void onCancelClick() {
			}
		});
		dialog.show();
	}
	
	protected void updateList() {
		
	}
}
